<?php

class Service_urls extends MY_Service
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Model_urls');
        $this->load->model('Model_rec_urls');
        $this->load->library('EmojiUtil');
        $this->load->driver('cache');
    }

    public function add($url)
    {
        $data = [
            'x_url' => $url,
            'url' => $url,
            'title' => '',
            'pinyin' => '',
            'keywords' => '',
            'cate' => '',
            'icon' => $url . '/favicon.ico',
            'icon_sina' => '',
            'remark' => '',
            'not_ad' => 0
        ];
        return $this->Model_urls->insert($data);
    }

    public function getInfo($cond, $realUrl = false)
    {
        $info = $this->Model_urls->getInfo($cond);
        if ($info && !$realUrl) {
            $info['url'] = '/site/redirect/' . $info['id'];
        }
        return $info ?: [];
    }

    public function get($id, $realUrl = false)
    {
        $info = $this->Model_urls->get($id);
        if ($info && !$realUrl) {
            $info['url'] = '/site/redirect/' . $info['id'];
        }
        return $info;
    }

    public function getRecUrls()
    {
        $rec_urls = $this->cache->memcached->get('urls_rec_list');
        if (empty($rec_urls)) {
            $rec_urls = $this->getRecList();
            $this->cache->memcached->save('urls_rec_list', $rec_urls, 86400);
        }
        return $rec_urls;
    }

    public function getRecList()
    {
        $cond = ['status' => 0];
        $result = $this->Model_rec_urls->getList(false, $cond);
        return $result;
    }

    public function getList($page = 1, $cond = [], $size = 40, $order = ['weight', 'DESC'])
    {
        $result = $this->Model_urls->getList($page, $cond, $size, 'id,title,url,IF(icon_sina="", icon, icon_sina) icon,remark,status,`show`', $order);
        foreach ($result as &$item) {
            $item['url'] = '/site/index/' . $item['id'] . '.html';
        }
        return $result;
    }

    public function getPagination($page = 1, $cond = [], $size = 40)
    {
        $result = $this->Model_urls->getList(false, $cond, $size, 'count(1) num');
        $num = $result[0]['num'];
        $totalPage = ceil($num / $size);
        return ['totalPage' => $totalPage, 'page' => $page];
    }

    public function add_weight($id)
    {
        list($flag, $siteViewNum) = $this->_cacheNum($id);
        if ($flag) {
            return true;
        }
        $info = $this->Model_urls->get($id);
        if (empty($info)) {
            return false;
        }
        $info['weight'] += $siteViewNum;
        $this->Model_urls->update($info);
    }

    private function _cacheNum($id)
    {
        $ids = [1];
        if (!in_array($id, $ids)) {
            return [false, 1];
        }

        $this->load->library('HtmlCacheHelper', ['dir' => 'cache/num/']);

        // 先写缓存，每10次入库一次
        $cacheKey = "n_" . $id;
        $siteViewNum = $this->htmlcachehelper->get($cacheKey);
        $siteViewNum = (int)$siteViewNum + 1;
        $this->htmlcachehelper->add($cacheKey, $siteViewNum, 86400);
        if ($siteViewNum < 10) {
            return [true, 0];
        }

        $this->htmlcachehelper->add($cacheKey, 0, 86400);
        return [false, $siteViewNum];
    }
}
